<template>
	<view class="dwu-datetime-picker">
		<view class="picker" @click="pickerShow">
			<u-input v-model="selectVal" inputAlign="right" border="none" :readonly="true" :disabled="disabled"
				:placeholder="placeholder"></u-input>
			<view class="arrow-down">
				<image src="@/static/news/arrow_right.png" mode=""></image>
			</view>
		</view>
		<u-datetime-picker ref="datetimePicker" :show="show" :minDate="minDate" :maxDate="maxDate" v-model="value1" :mode="mode" :formatter="formatter"
			@cancel="show = false" @confirm="confirm"></u-datetime-picker>
	</view>
</template>

<script>
	export default {
		name: "dwu-datetime-picker",
		props: {
			mode: {
				type: String,
				default: 'date'
			},
			placeholder: {
				type: String,
				default: '请选择日期'
			},
			disabled: {
				type: Boolean,
				default: false
			},
			defaultVal: {
				type: String,
				default: ''
			},
			minDate: {
				type: Number,
				default: Number(new Date()) - 2000000000000
			},
			maxDate: {
				type: Number,
				default: Number(new Date()) + 2000000000000
			}
		},
		watch: {
			defaultVal(val) {
				console.log(val, '111');
				this.value1 = Number(val)
				this.selectVal = uni.$u.timeFormat(val, 'yyyy-mm-dd')
			}
		},
		data() {
			return {
				show: false,
				selectVal: '',
				value1: Number(new Date())
			};
		},
		onReady() {
			this.$refs.datetimePicker.setFormatter(this.formatter)
		},
		methods: {
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
			pickerShow() {
				if (this.disabled) return
				this.show = true
			},
			confirm(e) {
				this.selectVal = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
				this.$emit('onconfirm', e)
				this.show = false
			}
		}
	}
</script>

<style lang="less">
	.dwu-datetime-picker {
		.picker {
			display: flex;
			align-items: center;

			.arrow-down {
				display: flex;
				align-items: center;

				image {
					width: 10rpx;
					height: 18rpx;
				}
			}
		}
	}
</style>